package cc.fine.svc.main.controller.tbl;


import cc.fine.svc.common.utils.R;
import cc.fine.svc.main.bean.dto.VideoInfoDto;
import cc.fine.svc.main.bean.vo.VideoUploadVo;
import cc.fine.svc.main.service.tbl.TblVideoService;
import cc.fine.svc.main.utils.MqUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyihe
 * @since 2023-10-25
 */
@RestController
@RequestMapping("/video")
@Api(description = "视频管理相关接口")
public class TblVideoController {


    @Autowired
    private TblVideoService videoService;

    @Autowired
    private MqUtils mqUtils;


    @PostMapping("/upload/{userId}")
    @ApiOperation("用户上传视频")
    public R uploadForUserId(@PathVariable String userId,
                             @RequestBody(required = false) VideoUploadVo videoVo) {
        videoService.upload(userId, videoVo);
        return R.ok();
    }

    @ApiOperation("获取一个视频的信息（包含点赞数、收藏数等等）")
    @GetMapping("/{videoId}")
    public R getInfo(@PathVariable String videoId) {
        VideoInfoDto dto = videoService.getInfo(videoId);
        return R.ok().data("data", dto);
    }

    @ApiOperation("关键字查找视频, 按点赞数降序，包含video+user+点赞、分享、评论、收藏数量")
    @GetMapping("/search/{page}/{limit}")
    public R search(
            @ApiParam("页数，从0开始") @PathVariable Integer page,
            @ApiParam("每页数据条数") @PathVariable Integer limit,
            String keyword) {
        List<Map<String, Object>> res = videoService.search(keyword, page, limit);
        return R.ok().data("data", res);
    }

    @ApiOperation("ip查找, 按点赞数降序，包含video+user+点赞、分享、评论、收藏数量")
    @GetMapping("/search/ip/{page}/{limit}")
    public R searchForIp(
            @ApiParam("页数，从0开始") @PathVariable Integer page,
            @ApiParam("每页数据条数") @PathVariable Integer limit,
            String ip) {
        List<Map<String, Object>> res = videoService.searchForIp(ip, page, limit);
        return R.ok().data("data", res);
    }

    @ApiOperation("获取用户发布的视频列表")
    @GetMapping("/video/publish/{userId}")
    public R videoPublishList(@PathVariable String userId) {
       List<VideoInfoDto> res = videoService.publishVideoInfoList(userId);
       return R.ok().data("data", res);
    }

    @ApiOperation("按标签查找视频")
    @GetMapping("/tag/{tagId}")
    public R getByTag(@PathVariable String tagId,
                      Integer page, Integer limit) {
        List<Map<String, Object>> res = videoService.getInfoByTagId(tagId, page, limit);

        return R.ok().data("data", res);
    }

    @ApiOperation("删除发布的视频")
    @DeleteMapping("/{videoId}")
    public R delete(@PathVariable String videoId) {
        videoService.removeVideo(videoId);

        return R.ok();
    }
}

